import {ApiResponse} from '@/services/http';
import {useEffect, useState} from 'react';

const useFetch = <T>(api: () => Promise<ApiResponse<T>>) => {
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState<T>();
  const [error, setError] = useState<boolean>(false);

  useEffect(() => {
    setLoading(true);
    api()
      .then(({data}) => setData(data))
      .catch(() => setError(true))
      .finally(() => setLoading(false));
  }, []);
  return {loading, data, error};
};
export default useFetch;
